<?php

class BookController extends Zend_Controller_Action
{
    public $_uid = 2; //
    public function init()
    {
       $category=new Admin_Model_MCategory();
       $bookCategory=$category->getAllCategories(array("_id","name"));
       $this->view->category=$bookCategory;

       $mySession=new Zend_Session_Namespace();
       if(isset ($mySession->uid) && isset($mySession->username))
       {
           $this->view->username=$mySession->username;
           $this->view->uid=$mySession->uid;
       }
       // uid = session('uid');

    }

    // xem sách theo danh mục sách
    public function indexAction()
    {
        $this->_helper->layout()->setLayout('layout_home_1');
        $request=$this->getRequest();
        $idCat=$request->getParam('type');
        $pagesize=10;
        $currentpage=$request->getParam('currentpage', 1);
        try
        {
            $currentpage=(int)$currentpage;
        }
        catch(Exception $e)
        {
            $currentpage=1;
        }
        
        $cat=new Admin_Model_MCategory();
        $this->view->catname=$cat->getCategoryName($idCat);
        if($this->view->catname=="")
        {
            $this->_redirect ("");
            return;
        }
        $this->view->catId=$idCat;

        $book=new Admin_Model_MBook();
        $total=0;
        $this->view->bookByCat=$book->getBookByCat($idCat,$pagesize,$currentpage,$total);
        $this->view->total=$total;
        $this->view->currentPage=$currentpage;
        $this->view->pagesize=$pagesize;
    }

    // xem chi tiết thông tin sách
    public function detailAction()
    {
        $this->_helper->layout()->setLayout('layout_home');

        $request=$this->getRequest();
        $idBook=$request->getParam('id');
        $uid='';
        
        $book=new Admin_Model_MBook();
        $bookDetail=$book->getBookDetailById($idBook);
        
        if(is_array($bookDetail) && count($bookDetail) ==0)
            $this->_redirect ("");
        else
        {
            //$user = new Personal_Model_MUser();
            //$user->storeViewHis($this->_uid, $bookDetail["_id
            $this->view->bookDetail=$bookDetail;		
            if($bookDetail["comment"] !=null)
            {
                $this->view->comments=$bookDetail["comment"];
            }
            if($bookDetail["rating"]!=null)
            {
                $this->view->rating=$bookDetail["rating"];
            }
            
            // thêm số view vào cuốn sách hiện tại
            $mySession=new Zend_Session_Namespace();
            if(isset ($mySession->uid) && isset($mySession->username))
            {
                $uid=$mySession->uid;
            }
            $book->addView($idBook, $uid);
        }

        // event
        $eventModel=new Community_Model_MEvent();
        $event=$eventModel->getEvents($total, 1, 5,40);
        $this->view->event=$event;

        // story
        $storyModel=new Community_Model_MStory();
        $story=$storyModel->getStories($total, 1, 5,40);
        $this->view->story=$story;

        // bookcomment
        $commentModel=new Community_Model_MBookComment();
        $comment=$commentModel->getComments($total, 1, 5,40);
        $this->view->comment=$comment;
     }

    
     // response -3  : khong tim thay sach
     // response -2  : khong tim thay trong book_view_history: id_user hoac id_book
     // response -1  : khong rating duoc vi ko dang nhap
     // response  0  : khong rating duoc
     // response  1  : rating thanh cong
     public function rateAction(){
        $uid='';
        $res = -1;
        $mySession=new Zend_Session_Namespace();
        if(isset ($mySession->uid) && isset($mySession->username))
        {
            $uid=$mySession->uid;
            $successful = 0;
            if($this->_getParam('bid','')!=''){
                $bid = $this->_getParam('bid','default');
                $rating =(int) $this->_getParam('rating',-1);
                $book = new Admin_Model_MBook();
                $res = $book->rateBook($uid, $bid, $rating);
            }
            else $res = 0;
        }
        //$res = $bid;
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->setLayout("layout_empty");
        $this->getResponse()->appendBody($res);
    }

    // xem thông tin tác giả
    public function authorAction()
    {
        $this->_helper->layout()->setLayout('layout_home');
    }
    
    // kết quả tìm kiếm sách
    public function searchAction()
    {
        $this->_helper->layout()->setLayout('layout_home_1');
        $request=$this->getRequest();
        try
        {
            $idCat=$request->getParam('type');
            $textSearch=$request->getParam('textsearch');
        }
        catch(Exception $e)
        {
            $this->_redirect("");
        }

        // xử lý loại được tìm kiếm
        if($idCat==null)
            $idCat='0';
        if($idCat!='0')
        {
            $cat=new Admin_Model_MCategory();
            $catName=$cat->getCategoryName($idCat);
            if($catName=="")
                $idCat='0';
            else
                $this->view->catname=$catName;
        }

        // phân trang
        $pagesize=10;
        $currentpage=$request->getParam('currentpage', 1);
        try
        {
            $currentpage=(int)$currentpage;
        }
        catch(Exception $e)
        {
            $currentpage=1;
        }
        $this->view->currentPage=$currentpage;
        $this->view->pagesize=$pagesize;

        // xử lý tìm kiếm
        $book=new Admin_Model_MBook();
        $total=0;
        $bookSearch=$book->searchBooks($idCat, $textSearch, $pagesize, $currentpage, $total);

        $this->view->total=$total;
        $this->view->bookSearch=$bookSearch;
        $this->view->idCat=$idCat;
        $this->view->textSearch=$textSearch;
    }


}

